package com.weaver;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileUtil {

    public static void main(String[] args) {
        String source = "D:\\泛微\\project\\svn\\file\\java\\source";
        String target = "D:\\泛微\\project\\svn\\file\\java\\source2";
        String path = "E:\\auto\\86e989f365be41a58d163cb2d563ff8c\\maxRev\\S上海蓝书信息";
        String path2 = "E:\\auto\\86e989f365be41a58d163cb2d563ff8c\\maxRev\\R软控股份";
//        boolean b = checkDirectory(path, path2, 0);
////        System.out.println("对比结果：" + b);
    }

    public static boolean checkDirectory(String source, String target, int type) {

        List<File> sourceList = getFileList(source);
        List<File> targetList = getFileList(target);

        if (sourceList == null||targetList == null ||sourceList.size() == 0 || targetList.size() == 0
                || sourceList.size() != targetList.size()) {
            return false;
        }

        if (!checkFileName(sourceList, targetList)) {
            return false;
        }
        if (type == 1) {
          return checkAllFile(sourceList, targetList, type);
        }

        return true;
    }


    public static List<File> getFileList(String path) {
        File file = new File(path);
        List<File> files = new ArrayList<File>();
        getAllFile(file, files);
        return files;
    }

    public static boolean checkAllFile(List<File> sourceList, List<File> targetList, int type) {

        for (File file : sourceList) {
            boolean result = compareFile(file, targetList, type);
            System.out.println("文件名：" + file.getName() + ",结果：" + result);
            if (result) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }

    public static boolean checkAllFile(List<File> sourceList, List<File> targetList,
                                       List<File> sourceList2, List<File> targetList2) {
        int equalsCount = 0;
        for (File sourceFile : sourceList) {
            File targetFile = getFileByName(sourceFile.getName(), targetList);
            File sourceFile2 = getFileByName(sourceFile.getName(), sourceList2);
            File targetFile2 = getFileByName(sourceFile.getName(), targetList2);
            if (targetFile == null) {
                if (targetFile2 == null) {
                    List<Change> changeList = getChangeList(readLine(sourceFile, 0), readLine(sourceFile2, 0));
                    if (changeList.size() == 0) {
                        equalsCount++;
                        continue;
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }

            List<String> sourceContents = readLine(sourceFile, 0);
            List<String> targetContents = readLine(targetFile, 0);
            List<Change> changeList = getChangeList(sourceContents, targetContents);

            List<String> sourceContents2 = readLine(sourceFile2, 0);
            List<String> targetContents2 = readLine(targetFile2, 0);

            List<Change> changeList2 = getChangeList(sourceContents2, targetContents2);

            if (changeList.size() != changeList2.size()) {
                return false;
            }
            int count = 0;
            for (Change sourceChange : changeList) {
                for (Change targetChange : changeList2) {
                    if (sourceChange.getContent().equals(targetChange.getContent())) {
                        if (sourceChange.getPreContent().equals(targetChange.getPreContent())) {
                            count++;
                            continue;
                        }else{
                            return false;
                        }
                    }
                }
            }

            if (count == changeList.size()) {
                equalsCount++;
                continue;
            }else{
                return false;
            }

        }

        if (equalsCount == sourceList.size()) {
            return true;
        }else{
            return false;
        }

    }

    public static File getFileByName(String name,List<File> targetList) {
        File targetFile = null;
        for (File file : targetList) {
            if (file.getName().equals(name)) {
                targetFile = file;
                break;
            }
        }
        return targetFile;
    }
    public static List<File> getAllFile(File file,List<File> fileList) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                if (!file1.isDirectory()) {
                    if (file1.getName().indexOf("_change") == -1) {
                        fileList.add(file1);
                    }
                }else {
                    getAllFile(file1,fileList);
                }
            }

        }
        return fileList;
    }


    public static boolean compareFile(File file, List<File> targetFiles, int type) {
        List<String> sourceContent = new ArrayList<String>();
        List<String> targetContent = new ArrayList<String>();
        sourceContent = readLine(file, type);
        if (sourceContent.size() == 0) {
            return false;
        }
        for (File file1 : targetFiles) {
            if (file1.getName().equals(file.getName())) {
                targetContent = readLine(file1, type);
            }
        }
        if (sourceContent.size() != targetContent.size() && type == 0) {
            return false;
        }

        List<Change> changeList = getChangeList(sourceContent, targetContent);
        if (changeList.size() == 0) {
            return true;
        } else {
            if (type == 1) {
                return checkDiff(file, changeList);
            }
        }
        return false;
    }

    public static boolean checkDiff(File file, List<Change> changeList) {
        String[] suffix = {".jsp", ".java"};
        String fileSuffix = file.getName().substring(file.getName().lastIndexOf("."), file.getName().length());
        if (!Arrays.asList(suffix).contains(fileSuffix)) {
            return false;
        }
        String outString = "public class Test { " + "\r\n" + " public void test(){" + "\r\n";
        String fileName = file.getAbsolutePath().replace(".java", "_change.java")
                .replace(".jsp", "_change.java");
        for (int i = 0; i < changeList.size(); i++) {
            if (changeList.get(i).getContent().indexOf("<%@ page") != -1) {
                String replace = changeList.get(i).getContent().replace("<%@ page", "")
                        .replace(" %>", "")
                        .replace("=", " ")
                        .replace("\"", "") + ";";
                outString = replace + "\r\n" + outString + "\r\n";
                continue;
            }
            if (changeList.get(i).getContent().startsWith("import")) {
                outString = changeList.get(i) + "\r\n" + outString + "\r\n";
                continue;
            }
            outString += changeList.get(i) + "\r\n";
            if (i == changeList.size() - 1) {
                outString += "}}";
                write(outString, fileName);
            }
        }
        return JdtUtil.scanFile(fileName);
    }

    public static List<Change> getChangeList(List<String> sourceContent, List<String> targetContent) {
        List<Change> changeList = new ArrayList<Change>();
        int index = 0;
        for (int i = 0; i < targetContent.size(); i++) {
            String s1 = targetContent.get(i);
            for (int j = index; j < sourceContent.size(); j++) {
                String s2 = sourceContent.get(j);
                if (s1.equals(s2)) {
                    index += 1;
                    break;
                }
                if (j == sourceContent.size() - 1) {
                    int pre;
                    if (i == 0) {
                        pre = 0;
                    }else{
                        pre = i - 1;
                    }
                    changeList.add(new Change(i,s1,targetContent.get(pre)));
                }
            }
        }
        return changeList;
    }

    public static boolean checkFileName(List<File> sourceList, List<File> targetList) {
        String[] sourceFileNames = new String[sourceList.size()];
        String[] targetFileNames = new String[targetList.size()];
        for (int i = 0; i < sourceList.size(); i++) {
            sourceFileNames[i] = sourceList.get(i).getName();
            targetFileNames[i] = targetList.get(i).getName();
        }
        for (String fileName : sourceFileNames) {
            //如果是sql文件，名字可以不一样
            if (fileName.indexOf(".sql") != -1) {
                continue;
            }
            if (Arrays.asList(targetFileNames).contains(fileName)) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }


    public static List<String> readLine(File file, int type) {
        if (file == null) {
            return null;
        }
        List<String> arrayList = new ArrayList<String>();
        try {
            FileReader fr = new FileReader(file);
            BufferedReader bf = new BufferedReader(fr);
            String str;
            // 按行读取字符串
            while ((str = bf.readLine()) != null) {
                if (type == 0) {
                    str = str.replace(" ", "");
                }

                if (str.trim().equals("") || str.trim().startsWith("/") || str.trim().startsWith("*")) {
                    continue;
                }
                arrayList.add(str);
            }
            bf.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return arrayList;
    }


    public static void write(String result, String toFile) {
        Writer w = null;
        try {
            w = new FileWriter(new File(toFile));
            w.write(result);
            w.flush();
            w.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                w.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
